<?php
/**
 * @Author: CJX
 * @Date:   2016-01-05 18:16:57
 * @Last Modified by:   hongxuan
 * @Last Modified time: 2016-05-06 05:11:09
 */

/**
 * 前台首页控制器
 */
Class IndexAction extends Action{
	/**
     * 回复消息或验证微信
     * @return [type] [description]
     */
    public function index(){
    	$echostr = $_GET['echostr'];
		if(!isset($_GET["echostr"])){
			$this->responseMsg();
		}else{
			$this->checkSignature();
		}
    }

    /**
     * 验证signature
     * @return [type] [description]
     */
    public function checkSignature(){
    	// token、nonce、timestamp按字典排序后拼接
		$token     = "hongxuan";
		$nonce     = $_GET['nonce'];
		$timestamp = $_GET['timestamp'];
		$signature = $_GET['signature'];
		$tmpArr    = array($token, $nonce, $timestamp);
		sort($tmpArr);
		$tmpStr    = implode($tmpArr);
		// 再sha1加密
		$tmpStr    = sha1($tmpStr);
    	// 若等于signature，则是微信的信息
    	if($tmpStr == $signature){
    		$echostr = $_GET['echostr'];
    		echo $echostr;
    		exit;
    	}
    }

    /**
     * 创建自定义菜单
     * @return [type] [description]
     */
    public function addMenu(){
		// 应用ID
    	// $appid = "wx5d9ccfa6be2a9b53";
    	$appid = "wxe3ab8ff4359517ff";
    	// 应用密钥
    	// $secret = "33c526f08023fc446707a6c148d4e189";
    	$secret = "f91af7f72044b8b52c85e9ebe1917137";
    	// 菜单
    	$button = '{
				"button":[
				{
					"name":"小功能",
					"sub_button":[
						{
							"type":"click",
							"name":"快递查询",
							"key":"快递查询"
						},
						{
							"type":"click",
				   			"name":"公交地图",
							"key":"公交地图"
						},
						{
							"type":"view",
							"name":"天气预报",
							"url":"http://m.hao123.com/a/tianqi"
						}
					]
				},
				{
					"type":"view",
					"name":"微官网",
					"url":"http://wx-microwebsite.coding.io/index.php"
				},
				{
					"name":"不许无聊",
					"sub_button":[
						{
							"type":"click",
							"name":"谁是卧底",
							"key":"谁是卧底"
						},
						{
							"type":"click",
							"name":"2048",
							"key":"game2048"
						}
					]
			    }]
			}';
		$MenuModel = new MenuModel;
    	$MenuModel->createMenu($appid, $secret, $button);
    }

    /**
     * 发送信息
     * @return [type] [description]
     */
    public function responseMsg(){
    	$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];

    	if(!empty($postStr)){
    		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);

    		// 接收的消息类型
    		$RX_TYPE = strtolower(trim($postObj->MsgType));
    		// 实例化模型
    		$IndexModel = new IndexModel;
    		$responseStr = '';

    		switch ($RX_TYPE) {
    			// 事件类型
    			case 'event':
    				switch ($postObj->Event) {
    					// 关注subscribe事件
    					case 'subscribe':
							$content     = '欢迎关注我们的公众号\\n微信号ID：'.$postObj->ToUserName;
							$responseStr = $IndexModel->responseSubscribe($postObj, $content);
    						break;

    					case 'click':
    						# code...
    						break;

    					default:
    						$responseStr = '';
    						break;
    				}
    				break;

    			// 收到文本消息
    			case 'text':
					$content     = '你发送的内容为：'.$postObj->Content;
					// 回复文本消息
					$responseStr = $IndexModel->responseText($postObj, $content);
    				break;

    			// 收到图片消息
    			case 'image':
    				// $responseStr = $IndexModel->$responseImage($postObj, $mediaId);
    				break;

    			default:
    				$responseStr = '';
    				break;
    		}
    		echo $responseStr;
    	}else{
			$foods = M('foods')->order('id DESC')->limit(3)->field('id,foodname,extra,price,discount,imageurl,status')->select();
			// 修改图片的路径
			foreach ($foods as $key => $value) {
				$path = explode('./',$value['imageurl']);
				$foods[$key]['imageurl'] = __ROOT__ . '/' . $path[1];
			}
			$this->assign('foods',$foods);
			$this->display();
    		exit;
    	}
    }

	/**
	 * 显示首页
	 * @return [type] [description]
	 */
	// public function index(){
	// 	$foods = M('foods')->order('id DESC')->limit(3)->field('id,foodname,extra,price,discount,imageurl,status')->select();
	// 	// 修改图片的路径
	// 	foreach ($foods as $key => $value) {
	// 		$path = explode('./',$value['imageurl']);
	// 		$foods[$key]['imageurl'] = __ROOT__ . '/' . $path[1];
	// 	}
	// 	$this->assign('foods',$foods);
	// 	$this->display();
	// }

	/**
	 * 显示关于我们页面
	 * @return [type] [description]
	 */
	public function aboutus(){
		$this->display();
	}

	/**
	 * 注销登录
	 * @return [type] [description]
	 */
	public function logout(){
		unset($_SESSION['user_id']);
		unset($_SESSION['user_name']);
		unset($_SESSION['user_email']);
		unset($_SESSION['login_time']);
		unset($_SESSION['login_ip']);
		//session_unset();
		//session_destroy();
		$this->redirect('Index/Index/index');
	}
}
